import 'package:demo/pages/home/home_cubit.dart';
import 'package:demo/pages/home_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../home_index/home_index.dart';
import '../home_mine/home_mine.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeCubit(),
      child: BlocBuilder<HomeCubit, HomeState>(
        builder: (BuildContext context, HomeState state) {
          return SafeArea(
            top: false,
            child: Scaffold(
              body: const [HomeIndexPage(), Home2Page(), HomeIndexPage(), HomeMinePage()][state.currentIndex],
              bottomNavigationBar: BottomNavigationBar(
                items: const <BottomNavigationBarItem>[
                  BottomNavigationBarItem(icon: Icon(Icons.home), label: ('首页')),
                  BottomNavigationBarItem(icon: Icon(Icons.chat), label: ('好友')),
                  BottomNavigationBarItem(icon: Icon(Icons.description), label: ('文章')),
                  BottomNavigationBarItem(icon: Icon(Icons.shop), label: ('我的')),
                ],
                currentIndex: state.currentIndex,
                selectedItemColor: Colors.redAccent,
                showUnselectedLabels: true,
                type: BottomNavigationBarType.fixed,
                unselectedItemColor: Colors.black45,
                onTap: (index) {
                  debugPrint('index  $index');
                  context.read<HomeCubit>().changeNavigationBarIndex(index);
                },
              ),
            ),
          );
        },
      ),
    );
  }
}
